#!/usr/bin/env python
# -*- coding: utf-8 -*-

# TODO: Clean this code
# TODO: Move it to pyglet 1.1


import pyglet

win = pyglet.window.Window(fullscreen=False)
ft = pyglet.font.load('Khmer OS Freehand Rapid', 36)

# s = unicode(open('main.py', 'r').read())
s = u"""
Hello! Pleasy type something.
"""

doc = pyglet.text.document.FormattedDocument(s)
doc.set_style(0, 20000, {'font_name': 'Andale Mono', 'font_size': 12, 'color': (0,0,0,255)})
font = doc.get_font(10)

pad = 30
txt = pyglet.text.layout.IncrementalTextLayout(doc, 
    win.width - pad * 2, 
    win.height - pad * 2, 
    multiline=True) #, dpi=None, batch=None, group=None)

txt.x = pad
txt.y = win.height - 2 * pad# - txt.height


@win.event
def on_text(c):
    global s
    doc.insert_text(len(s), c)
    s += c


bkg_col = pyglet.image.SolidColorImagePattern(color=(255,255,255,0))
bkg_img = bkg_col.create_image(win.width, win.height)
# event_loop = pyglet.app.EventLoop()
while not win.has_exit:
    win.dispatch_events()
    win.clear()

    bkg_img.blit(0,0)
    txt.draw()

    win.flip()


